'use strict';

module.exports = {
	/**
	 * 该功能用于接入百度广告，百度营销通里新增小程序授权时，在全场景模式下获取urlScheme地址
	 * /client/user/pub/create_urlscheme
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, currentTime, originId, path, query } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		console.log("data=", data);
		// originId = "gh_ea18be58ac0e";
		if (!originId) {
			return "originId不正确";
		}

		//通过originId查询对应的appsecret,此处注意originid的大小写，数据库里是小写i，传过来的是大写I
		const app = await db.collection('a_appid').where({ originid: originId }).get()
		let appid = app.data[0].appid;
		let appsecret = app.data[0].appsecret;

		if (!appid || !appsecret) {
			return "appid或appsecret不正确";
		}

		let generateRes = await vk.openapi.weixin.urlscheme.generate({
			appid: appid, //传入appid
			appsecret: appsecret, //传入appsecret
			jump_wxa: {
				path: path || "pages/index/index",
				query: query || "",
				env_version: "release", // 要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"。默认是正式版。
			},
			is_expire: true,
			iexpire_type: 1,
			expire_interval: 30, // 有效期30天
		});
		console.log("generateRes=", generateRes.openlink);
		//以下为满足百度营销通的返回格式要求，不要改。
		//https://fe-resource.cdn.bcebos.com/vector/images/wechat/%E5%AF%B9%E5%A4%96%E8%8E%B7%E5%8F%96urlScheme%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.pdf
		res.msg = "success";
		res.result = { scheme: generateRes.openlink }

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
